<?php
require_once('../classes/smarty_setup.php');
require('../classes/mofachi.Article.php');
$smarty = new Smarty_mofachi();

@ $act = $_REQUEST[ "act" ];
switch( $act ) {
	case "add": addArticle(); break;
	case "remove": removeArticle(); break;
	case "get": getArticle( $smarty ); break;
	case "update": updateArticle(); break;
	case "vote": voteArticle(); break;
}
$smarty->assign( 'act', $act );

listArticle( $smarty );
$smarty->display('t05_article.htm');

function listArticle( $smarty ) {
	$result = Article::getRecent( 0, 10 );
	$smarty->assign( 'res', $result );
}
function addArticle() {
	@ $source = $_POST[ 'source' ];
	@ $writer = $_POST[ 'writer' ];
	@ $title = $_POST[ 'title' ];
	@ $content = $_POST[ 'content' ];
	if( !$content || !$title || !$source )
		return;
	
	$article = new Article();
	$article->source = $source;
	$article->writer = $writer;
	$article->title = $title;
	$article->content = $content;
	$article->add();
}
function removeArticle() {
	@ $delist = $_POST[ "delist" ];
	if( !$delist )
		return;

	$id_array = explode( ",", $delist );
	foreach( $id_array as $key => $value ) {
		$article = new Article();
		$article->article_id = $value;
		$article->remove();
	}
}
function getArticle( $smarty ) {	
	@ $aid = $_GET[ "aid" ];
	if( !$aid )
		return;
	$article = new Article();
	$article->article_id = $aid;
	$article->loadById();
	$smarty->assign( 'a', $article );
}
function updateArticle() {
	@ $aid = $_POST[ 'aid' ];
	@ $source = $_POST[ 'source' ];
	@ $writer = $_POST[ 'writer' ];
	@ $title = $_POST[ 'title' ];
	@ $content = $_POST[ 'content' ];
	if( !$content || !$title || !$source )
		return;
	
	$article = new Article();
	$article->article_id = $aid;
	$article->loadById();
	$article->source = $source;
	$article->writer = $writer;
	$article->title = $title;
	$article->content = $content;
	$article->update();
}
function voteArticle() {
	@ $aid = $_GET[ 'aid' ];
	@ $yn = $_GET[ 'yn' ];
	if( !$aid || $yn == null )
		return;
	
	$article = new Article();
	$article->article_id = $aid;
	if( $yn )
		$article->voteYes();
	else
		$article->voteNo();
}
?>